<template>
    <el-container class="home_container">
      <el-header>
        <div>
          <img src="../assets/u=3399426401,1982560961&fm=26&gp=0.jpg" alt="">
          <span>电商后台管理系统</span>
        </div>
        <el-button type='info' @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="floding? '64px' : '200px'">
          <div class="flod" @click="flod_button">|||</div>
          <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened='true'
          :collapse='floding' :collapse-transition='false' router :default-active = 'activPath'>
          <!-- 一级导航栏 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级导航菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveState('/' + subItem.path)">
                <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconsObj: {
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        103: 'iconfont icon-tijikongjian',
        125: 'iconfont icon-user',
        145: 'iconfont icon-baobiao'
      },
      floding: false,
      activPath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activPath = window.sessionStorage.getItem('activPath')
  },
  methods: {
    // 点击退出的处理函数
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.menuList = res.data
    },
    flod_button () {
      this.floding = !this.floding
    },
    // 保存链接的激活状态
    saveState (activPath) {
      window.sessionStorage.setItem('activPath', activPath)
      this.activPath = activPath
    }
  }
}
</script>

<style lang="less" scoped>
.home_container{
  height: 100%;
}
.el-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  font-size: 20px;

  > div{
    display: flex;
    justify-content: space-between;
    align-items: center;

    img{
      width: 60px;
      height: 100%;
      padding-right: 10px;
    }
  }
}
.el-aside{
  background-color: #333744;
  .el-menu{
    border-right: none;
  }
}
.el-main{
  background-color: #eaedf1;
}
.iconfont{
  padding-right: 10px;
}
.flod{
  background-color: #1a2b3c;
  text-align: center;
  line-height: 24px;
  letter-spacing: 3px;
  font-size: 10px;
  color: #fff;
  cursor: pointer;
}
</style>
